package com.lee.mysql.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lee.mysql.MysqlApplication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import java.io.*;
import java.net.URISyntaxException;


public class JsonUtil {
    private static final Logger log = LoggerFactory.getLogger(JsonUtil.class);

    /** 读取JSON文件转换为字符串 */
    public static String readJSONStr(String filePath){
        String str = null;
        try {
            ClassPathResource resource = new ClassPathResource(filePath);
            InputStream is = resource.getInputStream();
            str = getInputStreamStr(is);
        } catch (IOException e) {
            log.error("Json转换出错",e);
        }

        return str;
    }


    /** 读取JSON文件转换为JSONObject */
    public static JSONObject getJSONObject(String filePath) {
        String str = null;
        try {
            ClassPathResource resource = new ClassPathResource(filePath);
            InputStream is = resource.getInputStream();
            str = getInputStreamStr(is);
        } catch (IOException e) {
            log.error("Json转换出错",e);
        }

        return JSONObject.parseObject(str);
    }


    /** 读取JSON文件转换为JSONArray */
    public static JSONArray getJSONArray(String filePath) {
        String str = null;
        try {
            ClassPathResource resource = new ClassPathResource(filePath);
            InputStream is = resource.getInputStream();
            str = getInputStreamStr(is);
        } catch (IOException e) {
            log.error("Json转换出错",e);
        }

        return JSONArray.parseArray(str);
    }


    /** 读取InputStream转换为字符串 */
    public static String getInputStreamStr(InputStream is){
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuilder s = new StringBuilder();
        String line;
        try {
            while ((line=br.readLine()) != null){
                s.append(line);
            }

        }catch (Exception e){
            log.error("Json转换出错",e);
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                log.error("IO流关闭出错",e);
            }
            try {
                is.close();
            } catch (IOException e) {
                log.error("IO流关闭出错",e);
            }
        }

        return s.toString();
    }



    /**
     * 获取项目根路径
     */
    public static String getProjectRootPath() {
        String result = null;
        try {
            result = new File(MysqlApplication.class.getResource("/").toURI().getPath()).getParentFile()
                    .getParentFile().getCanonicalPath();
        } catch (URISyntaxException e) {
            log.error("获取路径出错",e);
        } catch (IOException e) {
            log.error("获取路径IO出错",e);
        }

        if (result != null && !"".equals(result.trim())) {
            return result;
        } else {
            throw new RuntimeException("未找到项目根路径");
        }
    }


}
